const path = require("node:path");
const os = require("node:os")
const { exec } = require('child_process');
const express = require("express");
const app = express();

const coreConfig = require("../config/config");
const { initSql } = require("../node/db/db");
initSql();

global.__base = path.join(__dirname, "./");
const PORT = coreConfig.serverPort;

// static
app.use("/static", express.static(path.join(__dirname, "../core/static")));
app.use("/projects", express.static(path.join(__dirname, "../core/projects")));
// html
app.get(/[a-z]+\.html$/, express.static(path.join(__dirname, "../core")));
// index
app.get("/", (req, res) => {
    res.redirect("index.html")
})

app.use("/user", require("./routers/userController"));
app.use("/company", require("./routers/companyController"));
app.use("/project", require("./routers/projectController"));
app.use("/item", require("./routers/itemController"));
app.use("/bookmark", require("./routers/bookmarkController"));
app.use("/config", require("./routers/configController"));
app.use("/db", require("./routers/dbController"));

// listen
app.listen(PORT, () => {
    console.log(`App listening on port ${PORT}..`);

    // Open Brower
    let platformName = os.platform();
    if (platformName == "win32") {
        exec('rundll32 url.dll,FileProtocolHandler http://localhost:'+ PORT +'/');
    } else if (platformName == "linux") {
        exec("chrome http://localhost:" + PORT);
    }
})
